require "Common/define"
require "Logic/CtrlManager"
require "Logic/Config"
require "proto/trials/message/ReqTrialsBattleMessage"
require "proto/trials/message/ReqTrialsFriendRankMessage"
require "proto/trials/message/ReqTrialsInfoMessage"
require "proto/trials/message/ReqTrialsWorldRankMessage"

TrialHandel = TrialHandel or {};
G.serverData = G.serverData or {};

---------------------------S2C-----------------------------------

--Boss战斗返回
function TrialHandel.ResTrialsBattle(msg)
    --[[if msg.state == 0 then
        FireEvent(Config.EventType.Trials_Chaos_Battle, msg);
    end]]
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:"..msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError,"战斗返回失败")
        return
    end
    TrialsManager.SetBattleInfo(msg)
    FightManager.InitRewards(msg.items)
    if msg.dayReward and #msg.dayReward > 0 then
        -- FightManager.InitRewards(msg.dayReward)
        FightManager.InitExtraRewards(msg.dayReward)
    else
        FightManager.InitExtraRewards(nil)
    end
    
    BattlePlaybackManager.RequestReport(msg.fightId,msg.fightResult,msg.record)
    --logicMgr.TrialsManager.SetBattleDamage(msg);
end

--返回boss战信息
function TrialHandel.ResTrialsInfo(msg)
    logicMgr.TrialsManager.SetTrialsInfo(msg);
    FireEvent(Config.EventType.Trials_Chaos_Info);
end

--返回好友排行
function TrialHandel.ResTrialsFriendRank(msg)
    logicMgr.TrialsManager.SetTrialsFriendRank(msg)
    FireEvent(Config.EventType.Trials_Chaos_Friend_Rank, msg);
end

--返回世界排行
function TrialHandel.ResTrialsWorldRank(msg)
    logicMgr.TrialsManager.SetTrialsRank(msg);
    FireEvent(Config.EventType.Trials_Chaos_Work_Rank);
end

--返回战斗记录
function TrialHandel.ResTrialsRecordInfo(msg)
    logicMgr.TrialsManager.SetTrialsRecordInfo(msg);
    FireEvent(Config.EventType.Trials_Chaos_Record_Info);
end
---------------------------C2S-----------------------------------

--请求Boss战斗
function TrialHandel.SendTrialsBattle(msg)
    logicMgr.TrialsManager.enterTrials();
    local rdtr = ReqTrialsBattleMessage.New();
    rdtr.trialsId = msg.trialsId;
    rdtr.formation = msg.formation[1];
    rdtr.levelIndex = math.max(0, msg.level - 1)
    rdtr.duringIndex = math.max(0, msg.duration - 1)
    rdtr.bossBuffs = msg.buffs
    sendReqMessage(rdtr);
end

--请求boss战信息
function TrialHandel.SendTrialsInfo(msg)
    local rdtr = ReqTrialsInfoMessage.New();
    sendReqMessage(rdtr);
end

--请求好友信息排行榜
function TrialHandel.SendTrialsFriendRank()
    local rdtr = ReqTrialsFriendRankMessage.New();
    sendReqMessage(rdtr);
end

--请求世界排行榜
function TrialHandel.SendTrialsWorldRank()
    local rdtr = ReqTrialsWorldRankMessage.New();
    rdtr.version = logicMgr.TrialsManager.version;
    sendReqMessage(rdtr);
end

--请求战斗记录
function TrialHandel.SendTrialsRecordInfo()
    local rdtr = ReqTrialsMyRecordsMessage.New();
    rdtr.version = logicMgr.TrialsManager.recordVersion;
    sendReqMessage(rdtr);
end
